<template>
  <van-row>
    <van-nav-bar title="获取联系人" left-text="返回" left-arrow @click-left="onClickLeft">
    </van-nav-bar>
    <van-row class="side-nav">
      <h1 class="vant-title">
        <img src="https://img.yzcdn.cn/vant/logo.png">
        <span>获得联系人列表</span>
      </h1>
      <van-row>
        <h5>获得联系人列表</h5>
        <van-contact-card
          v-for="item in contactList"
          :key="item.id"
          :name="item.displayName"
          :tel="item.phoneNumbers"
          type="edit"
          :editable="false"
        />
      </van-row>
      <van-button type="default" class="getPhoto" @click="getContacts">获取</van-button>
    </van-row>
  </van-row>

</template>

<script>

export default {
  name: 'Contacts',
  data () {
    return {
      contactList: [],
      chosenContactId: null,
      editingContact: {},
      showList: false,
      showEdit: false,
      isEdit: false,
      lists: [{
        name: '张三',
        tel: '13000000000',
        id: 0
      }],
      toast: null
    }
  },
  methods: {
    getContacts () {
      let _this = this
      let contactFileds = ['displayName', 'name', 'phoneNumbers', 'emails', 'address']
      let options = { filter: '', multiple: true }
      navigator.contacts.find(contactFileds, _this.success, _this.onError, options)
    },
    success (contacts) {
      var _this = this
      for (let i = 0; i < contacts.length; i++) {
        let itemContact = contacts[i]
        let item = {
          id: itemContact.id,
          displayName: itemContact.displayName,
          phoneNumbers: itemContact.phoneNumbers[0].value
        }
        _this.contactList.push(item)
      }
    },
    onError (err) {
      let _this = this
      _this.app = JSON.stringify(err)
      _this.toast.clear()
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
  .van-doc-demo-block__title{
    margin: 0;
    font-weight: 400;
    font-size: 14px;
    color: rgba(69,90,100,.6);
    text-align: left;
  }
  .side-nav{
    box-sizing: border-box;
    width: 100%;
    padding: 64px 10px 10px;
  }
  .side-nav .vant-desc{
    margin: 0 0 40px;
    color: #7d7e80;
    font-size: 14px;
    text-align: left;
  }
  .vant-title{
    font-weight: normal;
    -webkit-user-select: none;
    user-select: none;
    margin: 0 0 16px;
    text-align: left;
  }
  .side-nav .vant-title img {
    width: 36px;
  }
  .van-cell__title{
    text-align: left;
  }
  .comment{
    margin: 10px 0px;
  }
  .getPhoto{
    width: 100%;
  }
  #myImage{
    width: 100%;
  }
</style>
